package com.example.Nurse_Notifier;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.ArrayList;

public class StatsContainer extends BaseExpandableListAdapter {

    private ArrayList<StatsItem> fields;
    private LayoutInflater inflater;
    private XbeeDeviceContainer xbeeDeviceContainer;

    public StatsContainer(Context context, ArrayList<StatsItem> fields) {
        this.fields = fields;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return fields.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return fields.get(groupPosition).getItemParameters().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return fields.get(groupPosition).getItemName();
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return fields.get(groupPosition).getItemParameters().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(android.R.layout.simple_expandable_list_item_1, parent, false);
        }

        ((TextView) convertView).setText(getGroup(groupPosition).toString());
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
        }

        ((TextView) convertView).setText(getChild(groupPosition, childPosition).toString());
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int i, int i2) {
        return false;
    }

    public double getMaxTime(ArrayList<Double> list) {
        double max = 0;
        double val = 0;
        for (int i = 0; i < list.size(); i++) {
            val = list.get(i);
            if (max < val) {
                max = val;
            }
        }
        return max;
    }

    public double getMinTime(ArrayList<Double> list) {
        double min = 0;
        double val = 0;
        for (int i = 0; i < list.size(); i++) {
            val = list.get(i);
            if (min > val) {
                min = val;
            }
        }
        return min;
    }

    public double getMeanTime(ArrayList<Double> list) {
        double sum = 0;
        for (int i = 0; i < list.size(); i++) {
            sum += list.get(i);
        }
        return sum / list.size();
    }

    public int getDropCountersCnt() {
        return -1;
    }
}